﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Text;

public partial class Pages_Public_SendQuery : BasePage
{
    private String _SellType = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {            
            BindDropDownLists();            
        }
    }
    /// <summary>
    /// Bindis the Page Initialization Variables
    /// </summary>
    protected void BindPageInfo()
    {
        _SellType = WebUtil.GetQueryStringInString("Type");
        if (String.Compare(_SellType, "Sell", true) == 0)
        {
            trSellType.Visible = false;
            h4Heading.InnerHtml = "Sell Property";            
        }
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    /// <summary>
    /// Binds Dropdownlists for the initial request.
    /// </summary>
    protected void BindDropDownLists()
    {
        BindDropdownList.Countries(ddlCountryID);
        ///Bind Property Types        
        BtcHousingEntities context = new BtcHousingEntities();
        var properties = from P in context.PropertyTypes where P.BusinessTypeID == ConfigReader.DeveloperBusinessTypeID select P;
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("<table cellpadding='0' cellspacing='0' style='width:100%;'>");
        int i = 0;
        foreach (PropertyType property in properties)
        {
            if (i % 2 == 0)
                sb.AppendFormat("<tr>");
            sb.AppendFormat("<td>");
            sb.AppendFormat("<input type='checkbox' id='chkInterestedProperty_{0}' name='chkInterestedProperty' value='{0}'/><label for='chkInterestedProperty_{0}'>{1}</lable>", property.ID.ToString(), property.Name);
            sb.AppendFormat("</td>");
            if (i % 2 > 0)
                sb.AppendFormat("</tr>");
            i++;
        }
        sb.AppendFormat("</table>");
        divPropertyTypes.InnerHtml = sb.ToString();
    }
    /// <summary>
    /// Saves a new Lead
    /// </summary>
    protected void SaveLead()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        Lead entity = new Lead();

        entity.ContactName = txtContactName.Text;
        entity.ContactNumber = txtContactNumber.Text;
        entity.CountryID = ddlCountryID.SelectedValue.ToInt();
        entity.Message = txtMessage.Text;
        entity.Email = txtEmail.Text;
        if (String.Compare(_SellType, "Sell", true) == 0)
            entity.BuySellAdvertise = "S";
        else
            entity.BuySellAdvertise = rdblBuySellAdvertise.SelectedValue;//txtBuySellAdvertise.Text;
        entity.IsDeleted = false; //chkIsDeleted.Checked;
        entity.IsVerified = true; //chkIsVerified.Checked;
        entity.VerificationKey = System.Guid.NewGuid();        
        entity.Created = DateTime.Now;        
        entity.Modified = DateTime.Now;

        context.AddToLeads(entity);
        
        ///Add Leads For
        int[] propertyTypeIDs = GetSelectedPropertyIDs();
        foreach (int propertyTypeID in propertyTypeIDs)
        {
            LeadFor leadFor = new LeadFor();
            leadFor.PropertyTypeID = propertyTypeID;
            if (leadFor.PropertyTypeID == ConfigReader.OtherPropertyTypeID)
                leadFor.Other = txtOtherProperty.Text;
            entity.LeadFors.Add(leadFor);
        }
        context.SaveChanges();

        WebUtil.ShowMessageBox(divMessage, "Your Query has Submitted Successfully to our team for review.", false);
        pnlFormContainer.Visible = false;
    }

    private int[] GetSelectedPropertyIDs()
    {
        List<int> selectedProperties = new List<int>();
        String[] ids = Request.Form["chkInterestedProperty"].Split(',');
        foreach (String id in ids)
            selectedProperties.Add(id.ToInt());

        return selectedProperties.ToArray();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SaveLead();            
            return;
        }
    }   
}